﻿using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ST.Common.Util;
using ST.WX.Common;
using ST.WX.Common.Model;
using ST.WX.MP.QRCode.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace ST.WX.MP.QRCode
{
	/// <summary>
	/// 微信公众号二维码相关接口
	/// </summary>
	public class WXQRCodeApi
	{
		private readonly ILogger logger;
		private readonly WXBaseApi baseApi;

		public WXQRCodeApi(ILogger<WXQRCodeApi> _logger, WXBaseApi _baseApi)
		{
			logger = _logger;
			baseApi = _baseApi;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="scene_id">临时二维码时为32位非0整型，永久二维码时最大值为100000（目前参数只支持1--100000）</param>
		/// <param name="expire_seconds">该二维码有效时间，以秒为单位。 最大不超过2592000（即30天），微信临时的默认30秒。</param>
		/// <param name="limit">是否是永久二维码</param>
		/// <returns></returns>
		public WXQRCodeResult Create(string appID, string appSecret, int scene_id, int expire_seconds = 0, bool limit = true)
		{
			try
			{
				string paras = string.Empty;
				if (limit)
				{
					paras = "{" + (expire_seconds > 0 ? ("\"expire_seconds\": " + expire_seconds + ", ") : "") + "\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
				}
				else
				{
					paras = "{" + (expire_seconds > 0 ? ("\"expire_seconds\": " + expire_seconds + ", ") : "") + "\"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
				}
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + baseApi.GetAccessToken(appID, appSecret),
					paras);
				if (string.IsNullOrEmpty(respText))
				{
					throw new Exception("创建带参数二维码失败，返回内容为空");
				}
				var ret = JsonConvert.DeserializeObject<WXQRCodeResult>(respText);
				return ret;
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "创建带参数二维码失败");
			}
			return null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="scene_id">临时二维码时为32位非0整型，永久二维码时最大值为100000（目前参数只支持1--100000）</param>
		/// <param name="expire_seconds">该二维码有效时间，以秒为单位。 最大不超过2592000（即30天），微信临时的默认30秒。</param>
		/// <param name="limit">是否是永久二维码</param>
		/// <returns></returns>
		public WXQRCodeResult Create(string appID, string appSecret, string scene_str, int expire_seconds = 0, bool limit = true)
		{
			try
			{
				string paras = string.Empty;
				if (limit)
				{
					paras = "{" + (expire_seconds > 0 ? ("\"expire_seconds\": " + expire_seconds + ", ") : "") + "\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + scene_str + "\"}}}";
				}
				else
				{
					paras = "{" + (expire_seconds > 0 ? ("\"expire_seconds\": " + expire_seconds + ", ") : "") + "\"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\":\"" + scene_str + "\"}}}";
				}
				string respText = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + baseApi.GetAccessToken(appID, appSecret),
					paras);
				if (string.IsNullOrEmpty(respText))
				{
					throw new Exception("创建带参数二维码失败，返回内容为空");
				}
				return JsonConvert.DeserializeObject<WXQRCodeResult>(respText);
			}
			catch (WXException ex)
			{
				logger.LogError(ex, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
			}
			return null;
		}
	}
}
